<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>审批销售订单变更请求</title>
    <script type="text/javascript">
      var viewSalesStrategyRdsidualPaymentWin;
      Ext.onReady(function(){
        var saleOrderId = Ext.create('Ext.form.field.Hidden', {
          name: 'id'
        });

        var customerId = Ext.create('Ext.form.field.Hidden', {
          name: 'customer_id'
        });

        var salesStrategyId = Ext.create('Ext.form.field.Hidden', {
          name: 'sales_strategy_id'
        });
        
        //备注
        var desc = Ext.create('Ext.form.field.Text', {
          fieldLabel: '备注',
          name: 'description',
          readOnly:true,
          anchor: '95%'
        });

        var toCustomer = Ext.create('Ext.form.field.Text', {
          fieldLabel: '到客户',
          name: 'customer_name',
          readOnly:true,
          anchor: '95%'
        });

        //要求发货时间
        var actionDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '发货日期',
          name: 'action_date',
          format: 'Y-m-d', //格式化日期
          readOnly:true,
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: true,
          blankText: "发货日期必须选择",
          emptyText: '发货日期...',
          anchor: '95%'
        });
        
        //合同号
        var contractNumber = Ext.create('Ext.form.field.Text', {
          fieldLabel: '合同编号',
          readOnly:true,
          name: 'contract_number',
          anchor: '95%'
        });

        //联系人
        var linkman = Ext.create('Ext.form.field.Text', {
          fieldLabel: '联系人',
          name: 'linkman',
          readOnly:true,
          anchor: '90%'
        });
        
        //联系电话
        var phone = Ext.create('Ext.form.field.Text', {
          fieldLabel: '联系电话',
          name: 'phone',
          readOnly:true,
          anchor: '90%'
        });

        //税率
        var taxRate = Ext.create('Ext.form.field.Number', {
          fieldLabel: '税率(%)',
          name:"tax_rate",
          readOnly:true,
          allowDecimals: true,
          minValue: 0,
          maxValue: 100,
          anchor: '95%'
        });

        var deliveryAddress = Ext.create('Ext.form.field.Text', {
          fieldLabel: '送货地址',
          readOnly:true,
          name: 'delivery_address',
          anchor: '95%'
        });

        //总金额
        var sumAmount = Ext.create('Ext.form.field.Number', {
          fieldLabel: '变更前总金额',
          labelWidth:85,
          name:"amount",
          readOnly:true,
          allowBlank: false,
          allowDecimals: true,
          minValue: 0,
          decimalPrecision:2,
          anchor: '95%'
        });

        //变更后总金额
        var tempSumAmount = Ext.create('Ext.form.field.Number', {
          fieldLabel: '变更后总金额',
          labelWidth:85,
          name:"temp_amount",
          readOnly:true,
          allowBlank: false,
          allowDecimals: true,
          minValue: 0,
          anchor: '95%'
        });

        var saleType = Ext.create('Ext.form.field.Text', {
          fieldLabel: '销售类型',
          name: 'sale_type',
          readOnly:true,
          anchor: '95%'
        });

        var salesStrategy = Ext.create('Ext.form.field.Text', {
          fieldLabel: '销售策略',
          name: 'sales_strategy',
          readOnly:true,
          anchor: '95%'
        });

        var sellDept = Ext.create('Ext.form.field.Text', {
          fieldLabel: '业绩部门',
          readOnly:true,
          name: 'sell_department_name',
          anchor: '95%'
        });

        var sales = Ext.create('Ext.form.field.Text', {
          fieldLabel: '销售员',
          readOnly:true,
          name: 'sales_name',
          anchor: '95%'
        });

        var editor = Ext.create('Ext.form.field.Text', {
          fieldLabel: '制单员',
          readOnly:true,
          name: 'employee_name',
          anchor: '95%'
        });
        //--------------------------------------------------------------------------
        
        //销售订单详细信息Model
        Ext.define('SaleOrderDetailAuditEditModel', {
          extend : 'Ext.data.Model',
          fields : [{name: 'id'},
            {name: 'product_name',mapping:'product.name'},
            {name: 'product_number',mapping:'product.number'},
            {name: 'spec',mapping:'product.spec'},
            {name: 'quantity',type: 'int'},
            {name: 'unit_price',type: 'float'},
            {name: 'discount',type: 'float'},
            {name: 'amount',type: 'float'},
            {name: 'new_unit_price',type: 'float'},
            {name: 'new_quantity',type: 'int'},
            {name: 'new_discount',type: 'float'},
            {name: 'new_amount',type: 'float'},
            {name: 'balance_quantity',type: 'int'},
            {name: 'edit_status'},
            {
              name: 'is_suit',
              mapping:'product.is_suit',
              convert:function(value,record){
                if(value == "1"){
                  return "套装";
                }else if(value == "0"){
                  return "单品";
                }
              }
            },
            {name: 'description'}
          ]
        });
        //销售订单列表Grid Store
        var saleOrderDetailDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'SaleOrderDetailAuditEditModel',
          proxy : {
            type : 'ajax',
            extraParams:{'sale_order_id':<%=params[:sale_order_id]%>},
            url : '/sale_order/getSaleOrderDetailsById',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          autoLoad : true
        });

        //生成要订单的产品的Grid
        var saleOrderDetailGrid = Ext.create('Ext.grid.Panel', {
          frame:false,
          title:'变更前产品列表',
          store: saleOrderDetailDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 235
            },{
              header: '产品ID',
              dataIndex: 'product_id',
              width: 50,
              hidden: false
            },{
              header: '产品编号',
              dataIndex: 'product_number',
              width: 115
            },{
              header: '规格',
              dataIndex: 'spec',
              width: 75
            },{
              header: '单位',
              dataIndex: 'unit',
              width: 45
            },{
              header: '数量',
              dataIndex: 'quantity',
              align: 'right',
              width: 50
            },{
              header: '单价',
              dataIndex: 'unit_price',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 90
            },{
              header: '折扣',
              dataIndex: 'discount',
              renderer: Ext.util.Format.numberRenderer('0,000.000000'),
              width: 70
            },{
              header: '金额',
              dataIndex: 'amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 90
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 140
            }],
          width: 1000,
          height: 255        
        });

        Ext.define('SaleOrderDetailAuditEditModel', {
          extend : 'Ext.data.Model',
          fields : [{name: 'id'},
            {name: 'product_name',mapping:'product.name'},
            {name: 'product_number',mapping:'product.name'},
            {name: 'spec',mapping:'product.spec'},
            {name: 'quantity',type: 'int'},
            {name: 'unit_price',type: 'float'},
            {name: 'discount',type: 'float'},
            {name: 'amount',type: 'float'},
            {name: 'new_unit_price',type: 'float'},
            {name: 'new_quantity',type: 'int'},
            {name: 'new_discount',type: 'float'},
            {name: 'new_amount',type: 'float'},
            {name: 'balance_quantity',type: 'int'},
            {name: 'edit_status'},
            {
              name: 'is_suit',
              mapping:'product.is_suit',
              convert:function(value,record){
                if(value == "1"){
                  return "套装";
                }else if(value == "0"){
                  return "单品";
                }
              }
            },
            {name: 'description'}
          ]
        });

        
        //这是变更后将要出现的产品
        //销售订单列表Grid Store
        var saleOrderDetailEditDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'SaleOrderDetailAuditEditModel',
          proxy : {
            type : 'ajax',
            extraParams:{'sale_order_id':<%=params[:sale_order_id]%>},
            url : '/sale_order/getLockedSaleOrderDetailsById',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          pageSize : pageSize,
          autoLoad : true
        });

        //生成要订单的产品的Grid
        var saleOrderDetailEditGrid = Ext.create('Ext.grid.Panel', {
          frame:false,
          title:'变更后产品列表',
          store: saleOrderDetailEditDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 210
            },{
              header: '产品编号',
              dataIndex: 'product_number',
              width: 115,
              hidden:true
            },{
              header: '数量',
              dataIndex: 'quantity',
              align: 'right',
              width: 50
            },{
              header: '单价',
              dataIndex: 'unit_price',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 60
            },{
              header: '折扣',
              dataIndex: 'discount',
              renderer: Ext.util.Format.numberRenderer('0,000.000000'),
              width: 70
            },{
              header: '金额',
              dataIndex: 'amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 70
            },{
              header: '编后数量',
              dataIndex: 'new_quantity',
              align: 'right',
              width: 75
            },{
              header: '编后单价',
              dataIndex: 'new_unit_price',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 95
            },{
              header: '编后折扣',
              dataIndex: 'new_discount',
              width: 75
            },{
              header: '编后金额',
              dataIndex: 'new_amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 95
            },{
              header: '套装?',
              dataIndex: 'is_suit',
              width: 50
            },{
              header: '备注',
              dataIndex: 'description',
              width: 80
            },{
              header: '状态',
              dataIndex: 'edit_status',
              renderer: orderEditStatusRender,
              width: 50
            },{
              header: '产品ID',
              dataIndex: 'product_id',
              width: 50,
              hidden:true
            }],
          width: 935,
          height: 255
        });
 
        var productTabs = Ext.create('Ext.tab.Panel', {
          //renderTo:'tabs',
          activeTab: 0,
          resizeTabs:true, // turn on tab resizing
          minTabWidth: 115,
          enableTabScroll:true,
          //width:600,
          //height:250,
          defaults: {autoScroll:true}
        });

        productTabs.add(saleOrderDetailGrid).show();
        productTabs.add(saleOrderDetailEditGrid);
        
        //审批部分
        var auditingStatus = Ext.create('Ext.form.RadioGroup', {
          fieldLabel: '是否通过',
          allowBlank: false,
          //hideLabel:true,
          //style:'margin-left:100px;margin-top:20px',
          //columns : 7,
          anchor: '30%',
          vertical :false,
          items:[
            {boxLabel:'是',inputValue:'是',name:'auditing_status'},
            {boxLabel:'否',inputValue:'否',name:'auditing_status'}
          ]
        });

        var auditOpinion = Ext.create('Ext.form.TextArea', {
          fieldLabel: '审批意见',
          name: 'audit_opinion',
          allowBlank: false,
          anchor: '98%',
          height: 40
        });

        var viewResidualPayment = Ext.create('Ext.Button', {
          text: '查看余款',
          tooltip: '查看配赠余款。',
          iconCls: 'icon-save',
          handler: function(){
            viewSalesStrategyRdsidualPaymentWin = Ext.create('Ext.window.Window', {
              layout: 'fit',
              width: 550,
              height: 130,
              closeAction: 'destroy',
              plain: true,
              modal: true,
              constrain:true,
              //modal: true,
              title: '配赠余款',
              autoLoad: {
                url: "/sale_order/salesStrategyResidualPayment",
                scripts: true,
                params:{'customer_id': customerId.getValue(),'sales_strategy_id':salesStrategyId.getValue()}
              }
            });

            viewSalesStrategyRdsidualPaymentWin.resizable = false;
            viewSalesStrategyRdsidualPaymentWin.show();
          }
        });
        
        var submitButton = Ext.create('Ext.Button', {
          text: '提交审批',
          tooltip: '审批销售订单变更。',
          iconCls: 'icon-save',
          handler: function(){
            if (saleOrderFormPanel.form.isValid()) {
              this.disable();
              saleOrderFormPanel.form.submit({
                method: 'GET',
                success: function(result, response){
                  if (response.result.info == "成功") {
                    //添加成功后，隐藏窗口，并刷新Grid
                    saleOrderAuditForEditWin.destroy();
                  }
                  else {
                    Ext.MessageBox.alert('消息', response.result.info);
                  }
                },
                failure: function(result, response){
                  Ext.MessageBox.alert('提示', result.responseText);
                  saleOrderAuditForEditWin.destroy();
                }
              });
            }
            else {
              Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
            }
          }
        });

        //暂时保存按钮
        var closeButton = Ext.create('Ext.Button', {
          text: '关闭',
          tooltip: '关闭当前窗口，并且当前编辑的信息不能保存。',
          iconCls: 'exit',
          handler: function(){
            saleOrderAuditForEditWin.destroy();
          }
        });

        //采购申请的FormPanel
        var saleOrderFormPanel = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 75,
            labelAlign: 'right'
          },
          width: '1080',
          frame : true,
          url: '/sale_order/updateAuditingEditStatus',
          method: 'GET',
          reader: Ext.create('Ext.data.reader.Json',{
            model: 'SaleOrderModel',
            root: 'root'
          }),
          //defaultType: 'textfield',
          items: [{
              layout: 'column',xtype: 'container',anchor: '100%',labelAlign: 'right',
              items: [{
                  columnWidth: .6,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [toCustomer]
                }, {
                  columnWidth: .2,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [actionDate]
                }, {
                  columnWidth: .2,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [contractNumber]
                }]
            },{
              layout: 'column',
              xtype: 'container',  anchor: '100%',
              items: [{
                  columnWidth: .3,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [linkman]
                }, {
                  columnWidth: .3,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [phone]
                }, {
                  columnWidth: .2,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [editor]
                }, {
                  columnWidth: .2,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [taxRate]
                }]
            },{
              layout: 'column',
              xtype: 'container',  anchor: '100%',
              items: [{
                  columnWidth: .6,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [deliveryAddress]
                },{
                  columnWidth: .2,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [sellDept]
                },{
                  columnWidth: .2,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [saleType]
                }]
            },{
              layout: 'column',
              xtype: 'container',  anchor: '100%',
              items: [{
                  columnWidth: .6,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [desc]
                },{
                  columnWidth: .2,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [sales]
                },{
                  columnWidth: .2,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [salesStrategy]
                }]
            },{
              layout: 'column',
              xtype: 'container',  anchor: '100%',
              items: [{
                  columnWidth: 1,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  items: [productTabs]
                }]
            },{
              layout: 'column',
              xtype: 'container',  anchor: '100%',
              items: [{
                  columnWidth: 0.3,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  labelWidth: 90,
                  items: [sumAmount]
                },{
                  columnWidth: 0.3,
                  layout: 'anchor',
                  xtype: 'container',  anchor: '100%',
                  labelWidth: 90,
                  items: [tempSumAmount]
                }]
            },saleOrderId,{
              xtype: 'fieldset',
              //defaults: {width: 700},
              title: '审批(撤消)',
              border: true,
              items: [auditingStatus,auditOpinion]
            },customerId,salesStrategyId],
          buttons: [viewResidualPayment,submitButton,closeButton]
        });
        
        saleOrderFormPanel.getForm().load({
          url: '/sale_order/getSaleOrderById',//?id=' + <%=params[:sale_order_id]%>,
          params: {
            'id': <%=params[:sale_order_id]%>
          },
          //method : 'GET',
          waitMsg: '正在载入数据...',
          success: function(form, action){

          },
          failure: function(form, action){
            Ext.MessageBox.alert('提示信息', '信息加载失败');
          }
        });        
        saleOrderFormPanel.render('audit_sale_order_for_cancel_form');
      })
    </script>
  </head>
  <body>
    <div id="audit_sale_order_for_cancel_form"></div>
  </body>
</html>